﻿var app = angular.module('stuApp', ['ngResource','ngCookies']);
app.controller('errorCtr', function ($scope, parseService, errorService, $cookieStore) {
    $scope.username = $cookieStore.get("loginInfo").name;
    //筛选
    $scope.reason_num = -1;
    $scope.material_num = -1;
    $scope.scene_num = -1;
    $scope.type_num = -1;
    $scope.knowledge_num = -1;
    //筛选列表
    $scope.reasonList = [];
    $scope.materialsList = [];
    $scope.sceneList = [];
    $scope.typeList = [];
    $scope.knowledgeList = [];
    //筛选参数
    $scope.searchList = {
        "student_id": $cookieStore.get('loginInfo').empid,
        "reason_id": "",
        "material_id": "",
        "exercises_place": "",
        "exercises_type": "",
        "knowledge_id": "",
        "loadtype": 0,//?
        "pageindex":0,
    }
    //错题列表
    $scope.errorList = [];
    //错题页数
    $scope.page_total = 0;
    //主观原因保存参数
    $scope.saveinfo = {
        "student_id": "",
        "xt_exercises_id": "",
        "reason_id": "",
        "reason_text": "",
    }

    //巩固练习
    $scope.practice = function () {
        window.location.href ="/modules/student/tmpl/pass.html?chapterid=0"
    }
    //数据
    $scope.do_search = function () {
        errorService.getErrorList($scope.searchList, function (data) {
            $scope.errorList = data.data;
            $scope.set_page_total();
        })
    }
    //计算页数
    $scope.set_page_total = function () {
        if ($scope.errorList.length / 10 > parseInt($scope.errorList.length / 10)) {
            $scope.page_total = parseInt($scope.errorList.length / 10) + 1;
        } else if ($scope.errorList.length / 10 == parseInt($scope.errorList.length / 10)){
            $scope.page_total = parseInt($scope.errorList.length / 10);
        }
        $scope.page = new Array($scope.page_total);
    }
    
    //主观原因
    $scope.getReason = function () {
        errorService.Getxt_reason({}, function (data) {
            $scope.reasonList = data.data;
            $scope.getMaterial();
        })
    }
    //教材
    $scope.getMaterial = function () {
        var par_material = {
            "CourseName": "化学",
        };
        errorService.getMaterial(par_material, function (data) {
            $scope.materialsList = data.data;
            $scope.getScenes();
        })
    }
    //试题场景
    $scope.getScenes = function () {
        errorService.Getexercises_place({}, function (data) {
            $scope.sceneList = data.data;
            $scope.getType();
        })
    }
    //题型
    $scope.getType = function () {
        errorService.Getexercises_type({}, function (data) {
            $scope.typeList = data.data;
            $scope.getKnowledge();
        })
    }
    //知识点
    $scope.getKnowledge = function () {
        var par = {
            "student_id": $cookieStore.get('loginInfo').empid,
        }
        errorService.Getxt_error_knowledge(par, function (data) {
            $scope.knowledgeList = data.data;
            $scope.do_search();
        })
    }

    //选择主观原因
    $scope.reasonChosen = function (id, index) {
        $scope.searchList.reason_id = id;
        $scope.reason_num = index;
        $scope.do_search();
    }
    //选择教材
    $scope.materialChosen = function (id, index) {
        $scope.searchList.material_id=id
        $scope.material_num = index;
        $scope.do_search();
    }
    //选择试题场景
    $scope.sceneChosen = function (id, index) {
        $scope.searchList.exercises_place = id;
        $scope.scene_num = index;
        $scope.do_search();
    }
    //选择题型
    $scope.typeChosen = function (id, index) {
        $scope.searchList.exercises_type = id;
        $scope.type_num = index;
        $scope.do_search();
    }
    //选择知识点
    $scope.knowledgeChosen = function (id, index) {
        $scope.searchList.knowledge_id = id;
        $scope.knowledge_num = index;
        $scope.do_search();
    }

    //排序
    $scope.sortBy = function (num) {
        $scope.searchList.loadtype = num;
        $scope.do_search();
    }

    //错题原因
    $scope.selectReason = function (outindex, index) {
        if ($scope.errorList[outindex].xt_err_rea[index].istrue == 1) {
            $scope.errorList[outindex].xt_err_rea[index].istrue = 0;
        } else {
            $scope.errorList[outindex].xt_err_rea[index].istrue = 1;
        }
        $scope.saveinfo = {
            "student_id": $cookieStore.get('loginInfo').empid,
            "xt_exercises_id": $scope.errorList[outindex].exercises_id,
            "reason_id": $scope.errorList[outindex].xt_err_rea[index].reason_id,
            "reason_text": $scope.errorList[outindex].xt_err_rea[$scope.errorList[outindex].xt_err_rea.length-1].reason_text,
        }
        if ($scope.errorList[outindex].xt_err_rea[index].istrue == 1) {
            $scope.save();
        }
        else if ($scope.errorList[outindex].xt_err_rea[index].istrue == 0) {
            $scope.del();
        }
    }
    //错在哪
    $scope.set_reason_text = function (outindex) {
        $scope.saveinfo = {
            "student_id": $cookieStore.get('loginInfo').empid,
            "xt_exercises_id": $scope.errorList[outindex].exercises_id,
            "reason_id": 0,
            "reason_text": $scope.errorList[outindex].xt_err_rea[$scope.errorList[outindex].xt_err_rea.length - 1].reason_text,
        }
        $scope.save();
    }
    //插入错题信息
    $scope.save = function () {
        if ($scope.saveinfo.reason_text == null || $scope.saveinfo.reason_text.length <= 0) {
            $scope.saveinfo.reason_text = "";
        }
        parseService.Setstu_reason($scope.saveinfo, function (data) {
            if (data) {

            }
        })
    }
    //删除错题信息
    $scope.del = function () {
        if ($scope.saveinfo.reason_text == null || $scope.saveinfo.reason_text.length <= 0) {
            $scope.saveinfo.reason_text = "";
        }
        parseService.Delstu_reason($scope.saveinfo, function (data) {
            if (data) {

            }
        })
    }

    //选择页数
    $scope.choosenPage = function (index) {
        $scope.searchList.pageindex = index;
        $scope.do_search();
    }
    //上一页
    $scope.prev = function () {
        if ($scope.searchList.pageindex > 0) {
            $scope.searchList.pageindex--;
        }
    }
    //下一页
    $scope.next = function () {
        if ($scope.searchList.pageindex < $scope.page_total - 1) {
            $scope.searchList.pageindex++;
        }
    }
    $scope.init = function () {
        $scope.getReason();
        //$scope.do_search();
    }
    $scope.init();
})